【Unity基础】在2d的界面中获取鼠标的坐标 您所在的位置:网站首页 unity 获取坐标异常 【Unity基础】在2d的界面中获取鼠标的坐标

【Unity基础】在2d的界面中获取鼠标的坐标

2023-08-18 12:41| 来源: 网络整理| 查看: 265

思路:

主要用到了 RectTransformUtility里面的ScreenPointToLocalPointInRectangle函数

public static bool ScreenPointToLocalPointInRectangle

(RectTransform rect, ----- 希望得到坐标结果的父对象

Vector2 screenPoint, ----- 鼠标位置

Camera cam, ----- UI摄像机

out Vector2 localPoint); ----- 相对于父对象的坐标

Vector2 localPos; RectTransformUtility.ScreenPointToLocalPointInRectangle((RectTransform)GameObject.FindGameObjectWithTag("Bottom").transform,//希望得到坐标结果的父对象 (eventData as PointerEventData).position, // 鼠标位置 (eventData as PointerEventData).pressEventCamera, //UI摄像机 out localPos); nowDragImg.transform.localPosition = localPos;//相对于父对象的坐标

![在这里插入图片描述](https://img-blog.csdnimg.cn/20210607170636924.png



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有